<template>
  <div >    
    <!-- 添加或修改服务类目对话框 -->
    <el-dialog title="选择服务" :visible.sync="open" width="40%" append-to-body>
      <el-row :gutter="20">
        <el-col :span="8" >
          <el-card class="box-card">
            
            <el-table
              v-loading="loading"
              :data="categoryList"
              row-key="id"
              :height="600"
              :default-expand-all="false"
              highlight-current-row
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
              @row-click="handleCategoryRowClick"
            >
              <el-table-column label="类目名称" prop="name" >
                
              </el-table-column>
              
              
            </el-table>
          </el-card>
        
        </el-col>
        <el-col :span="16" >
          <el-card class="box-card">
            <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          
            <el-form-item label="项目名称" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入名称"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>         
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-form-item>
          </el-form>

          <el-table v-loading="attrLoading" :data="attributeList" @selection-change="handleSelectionChange" :height="500">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="项目名称" align="center" prop="name" width="250" show-overflow-tooltip />
            <el-table-column label="单位" align="center" prop="unit" width="50"/>
            <el-table-column label="指导价" align="center" prop="price" width="80" />
            
          </el-table>
          <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          
          </el-card>
          
        </el-col>
      </el-row>
    
      <div slot="footer"  class="dialog-footer" style="text-align:center;" >
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCategory, getCategory, delCategory, addCategory, updateCategory, exportCategory } from "@/api/business/category";
import { listAttribute, getAttribute, delAttribute, addAttribute, updateAttribute, exportAttribute } from "@/api/business/attribute";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import ImageUpload from '@/components/ImageUpload';
export default {
  name: "Category",
  components: {
    ImageUpload,
    Treeselect
  },
  data() {
    return {
      // 选中数组
      ids: [],
      selection: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      attrLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 服务类目表格数据
      categoryList: [],
      attributeList: [],
      // 服务类目树选项
      categoryOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: null,
        keywords: null,
        description: null,
        parentId: null,
        level: null,
      },
      queryAttrParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        name: null,
        value: null
      },
      total: 0,
      clickRow: null,
      // 表单参数
      form: {},
      attrform: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "类目名称不能为空", trigger: "blur" }
        ],
        
      }
    };
  },
  created() {
    this.getList();
    
  },
  methods: {
    init(){
      this.open = true;
    },
    /** 查询服务类目列表 */
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        this.categoryList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 查询服务分类参数列表 */
    getAttributeList() {
      this.attrLoading = true;
      this.queryAttrParams.categoryId = this.clickRow.id;
      listAttribute(this.queryAttrParams).then(response => {
        this.attributeList = response.rows;
        this.total = response.total;
        this.attrLoading = false;
      });
    },
    /** 转换服务类目数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
	/** 查询服务类目下拉树结构 */
    getTreeselect() {
      listCategory().then(response => {
        this.categoryOptions = [];
        const data = { id: 0, name: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.categoryOptions.push(data);
      });
    },
  
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        keywords: null,
        description: null,
        parentId: null,
        iconUrl: null,
        picUrl: null,
        level: null,
        sort: null
      };
      this.resetForm("form");
    },
    // 表单重置
    resetAttr() {
      this.attrform = {
        id: null,
        name: null,
        value: null,
        unit: null,
        price: null
      };
      this.resetForm("attrform");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selection = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() {
      if (this.single ) {
        this.msgSuccess("请选择一条记录");
        return;
      }
      this.open = false;
      this.$emit("finishSelected", this.selection[0]);      
    },
    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleCategoryRowClick(row) {
      this.clickRow = row;
      this.getAttributeList();
    },
    
  }
};
</script>
<style scoped>
  .el-button + .el-button {
      margin-left: 4px;
  }
  
</style>
